﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.Schedule
{
    public partial class EditSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int courseInstanceId = -1;
                if (int.TryParse(Request.QueryString["ID"], out courseInstanceId))
                {
                    int currentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                    CourseInstance courseInstance = Department.GetSchedule(currentDepartmentId).ScheduledCourses.FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);
                    if (courseInstance == null)
                    {
                        courseInstance = Department.GetSchedule(currentDepartmentId).ConflictedCourses.FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);
                        EditScheduleLabel.Text = "Currently editing Conflicted Item: " + courseInstance.Course.Number + " - " + courseInstance.SectionNumber;
                    }
                    else
                        EditScheduleLabel.Text = "Currently editing Scheduled Item: " + courseInstance.Course.Number + " - " + courseInstance.SectionNumber;

                    RoomDataSource.SelectParameters["courseid"].DefaultValue = courseInstance.Course.Id.ToString();
                    //RoomDropList.DataBind();
                }
                else
                    EditScheduleLabel.Text = "The ID for the schedule item you are trying to edit is invalid";
            }
            else if (HttpContext.Current.Session["EditScheduleLabeltext"] != null)
            {
                if (!EditScheduleLabel.Text.Contains((string)HttpContext.Current.Session["EditScheduleLabeltext"]))
                {
                    EditScheduleLabel.Text = (string)HttpContext.Current.Session["EditScheduleLabeltext"] + " - " + EditScheduleLabel.Text;
                    HttpContext.Current.Session["EditScheduleLabeltext"] = "";
                }
            }
            
        }
        
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.IsInRole("admin")) Response.Redirect("~/Schedule/Scheduledefault.aspx");
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton scheduleMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("ScheduleMenuButton");
            scheduleMenuButton.CssClass = "selected";
        }

        protected void OKButton_Click(object sender, EventArgs e)
        {
            if (FacultyDropList.SelectedValue == "" || RoomDropList.SelectedValue == "" || TimeSlotDropList.SelectedValue == "")
            {
                String errmsg = "None of the fields may be blank";
                if (!EditScheduleLabel.Text.Contains(errmsg))
                {
                    HttpContext.Current.Session["EditScheduleLabeltext"] = errmsg;
                    Server.Transfer("~/Schedule/EditSchedule.aspx", true);
                }
                return;
            }
            else {
                HttpContext.Current.Session["EditScheduleLabeltext"] = "";
            }

            int facultyId = Convert.ToInt32(FacultyDropList.SelectedValue);
            int roomId = Convert.ToInt32(RoomDropList.SelectedValue);
            int timeId = Convert.ToInt32(TimeSlotDropList.SelectedValue);

            Model1Container db = new Model1Container();

            int courseInstanceId = -1;
            if (int.TryParse(Request.QueryString["ID"], out courseInstanceId))
            {
                int currentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                Database.Schedule schedule = Department.GetSchedule(currentDepartmentId);
                CourseInstance courseInstance = db.Set<CourseInstance>().Include("Course").Include("TaughtBy").Include("Room").Include("TimeSlot").Include("Schedule").FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);

                courseInstance.TaughtBy = db.Set<Faculty>().FirstOrDefault(facultyToFind => facultyToFind.Id == facultyId);
                courseInstance.Room = db.Set<Room>().FirstOrDefault(roomToFind => roomToFind.Id == roomId);
                courseInstance.TimeSlot = db.Set<TimeSlot>().FirstOrDefault(timeSlotToFind => timeSlotToFind.Id == timeId);
                courseInstance.ScheduleIdforConflicted = null;
                courseInstance.ScheduleIdforScheduled = schedule.Id;

                db.SaveChanges();
            }

            HttpContext.Current.Session["EditScheduleLabeltext"] = "";
            EditScheduleLabel.Text = "Success";
            Response.Redirect("~/Schedule/Scheduledefault.aspx");
        }

        protected void FacultyDropList_OnDataBound(object sender, EventArgs e)
        {
            int courseInstanceId = -1;
            if (int.TryParse(Request.QueryString["ID"], out courseInstanceId))
            {
                int currentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                CourseInstance courseInstance = Department.GetSchedule(currentDepartmentId).ScheduledCourses.FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);
                if (courseInstance == null)
                {
                    courseInstance = Department.GetSchedule(currentDepartmentId).ConflictedCourses.FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);
                }

                foreach (ListItem item in FacultyDropList.Items)
                {
                    if (courseInstance.TaughtBy != null && item.Value == courseInstance.TaughtBy.Id.ToString())
                        item.Selected = true;
                }
            }
            else
                EditScheduleLabel.Text = "The ID for the schedule item you are trying to edit is invalid";
        }

        protected void RoomDropList_DataBound(object sender, EventArgs e)
        {
            int courseInstanceId = -1;
            if (int.TryParse(Request.QueryString["ID"], out courseInstanceId))
            {
                int currentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                CourseInstance courseInstance = Department.GetSchedule(currentDepartmentId).ScheduledCourses.FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);
                if (courseInstance == null)
                {
                    courseInstance = Department.GetSchedule(currentDepartmentId).ConflictedCourses.FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);
                }
                if (courseInstance.Room != null)
                {
                    foreach (ListItem item in RoomDropList.Items)
                    {
                        if (item.Value == courseInstance.Room.Id.ToString())
                            item.Selected = true;
                    }
                }
            }
            else
                EditScheduleLabel.Text = "The ID for the schedule item you are trying to edit is invalid";

            TimeSlotDropList.DataBind();
        }

        protected void TimeSlotDropList_DataBound(object sender, EventArgs e)
        {
            int courseInstanceId = -1;
            if (int.TryParse(Request.QueryString["ID"], out courseInstanceId))
            {
                int currentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                CourseInstance courseInstance = Department.GetSchedule(currentDepartmentId).ScheduledCourses.FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);
                if (courseInstance == null)
                {
                    courseInstance = Department.GetSchedule(currentDepartmentId).ConflictedCourses.FirstOrDefault(courseInstanceToFind => courseInstanceToFind.Id == courseInstanceId);
                }

                if (courseInstance.TimeSlot != null)
                {
                    ListItem item = TimeSlotDropList.Items.FindByValue(courseInstance.TimeSlot.Id.ToString());
                    if (item == null)
                    {
                        item = new ListItem();
                        item.Text = courseInstance.TimeSlot.ToString();
                        item.Value = courseInstance.TimeSlot.Id.ToString();
                        item.Selected = true;

                        TimeSlotDropList.Items.Add(item);
                    }
                    else item.Selected = true;
                }
            }
            else
                EditScheduleLabel.Text = "The ID for the schedule item you are trying to edit is invalid";
        }
    }
}